﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;

namespace Zwms.Client.InWms
{
    public partial class InBillWnd : UserControl
    {
        #region 成员变量
        private string _currentInPlanBillCode;
        BilInBill CurrentInBill { get; set; }
        #endregion

        #region 构造函数

        public InBillWnd()
        {
            InitializeComponent();
            InitBtnImage();
        }

        public InBillWnd(string sInPlanBillCode)
        {
            InitializeComponent();
            InitBtnImage();
            _currentInPlanBillCode = sInPlanBillCode;
            InitBill(sInPlanBillCode);
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnSelectInPlanBill.Image = global::Zwms.Client.Properties.Resources.view;
            this.btnReceive.Image = global::Zwms.Client.Properties.Resources.list;
        }
        private void InitBill(string sInPlanBillCode)
        {
            ServiceReferenceZwms.ServiceZwmsClient client
               = new ServiceReferenceZwms.ServiceZwmsClient();
            BilInPlanBill ipBillQueryRecord = new BilInPlanBill();
            ipBillQueryRecord.Id = sInPlanBillCode;
            BilInPlanBill ipBillRecord = client.QueryInPlanBill(ipBillQueryRecord);
            if (ipBillRecord == null)
            {
                MessageBox.Show("获取入库计划单失败！");
                return;
            }
            else
            {
                if (ipBillRecord.BillStatus == "0")
                {
                    MessageBox.Show("请首先打印该入库计划的条码标签！");
                    return;
                }
                //首先在列表中搜索是否已有该入库计划的入库单
                LoadInPlanBill(ipBillRecord);
            }
        }

        private void LoadInPlanBill(BilInPlanBill record)
        {
            tbInPlanBillCode.Text = record.Id;
            tbBillStatus.Text = CommonDefine.DictInBillStatus[record.BillStatus];

            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            BilInPlanBillDetail queryRecord = new BilInPlanBillDetail();
            queryRecord.IBPlanCode = record.Id;
            PagerIndex pagerIndex = new PagerIndex(1, int.MaxValue);
            IList<BilInPlanBillDetail> inBilDetailList = client.QueryInPlanBillDetailList(queryRecord, ref pagerIndex);

            this.bilInPlanBillDetailBindingSource.DataSource = null;
            if (inBilDetailList.Count > 0)
            {
                this.bilInPlanBillDetailBindingSource.DataSource = inBilDetailList;
            }
        }

        #endregion

        #region 控件函数
        private void btnSelectInPlanBill_Click(object sender, EventArgs e)
        {
            QueryInPlanBillWnd uc = new QueryInPlanBillWnd();
            ContainerForm window = new ContainerForm(uc, "选择入库计划单");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
               //判断权限
                if (MainForm.CurrentUser.UserRole.Trim() == "01" || MainForm.CurrentUser.UserRole.Trim() == "02")
                {
                    //无限制
                }
                else if (MainForm.CurrentUser.UserRole.Trim() == "03")
                {
                    //判断是否为该用户的入库计划单
                    if (uc.SelectedRecord.CreateUserCode != MainForm.CurrentUser.Id)
                    {
                        MessageBox.Show("对不起，您不具备该入库单的操作权限！");
                        return;
                    }
                }

                //载入入库计划
                _currentInPlanBillCode = uc.SelectedRecord.Id;
                InitBill(uc.SelectedRecord.Id);
            }
        }
       
        //扫描收货
        private void btnReceive_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrWhiteSpace(_currentInPlanBillCode))
            {
                MessageBox.Show("请首先选择入库计划单！");
                return;
            }

            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            BilInPlanBill record = new BilInPlanBill();
            record.Id = _currentInPlanBillCode;
            record = client.QueryInPlanBill(record);

            if (record == null)
            {
                MessageBox.Show("获取入库计划单信息失败！");
                return;
            }
            
            if (record.BillStatus == "0")
            {
                MessageBox.Show("请首先打印入库计划单条码！");
                return;
            }

            if (record.BillStatus == "2" || record.BillStatus == "3")
            {
                MessageBox.Show("入库计划单已完成收货！");
                return;
            }

             if (record.BillStatus == "4")
            {
                MessageBox.Show("入库计划单已撤销！");
                return;
            }

             BilInBill queryRecord = new BilInBill();
             queryRecord.IBPlanCode =_currentInPlanBillCode;
             CurrentInBill = client.QueryInBill(queryRecord);

             if (CurrentInBill == null)
             {
                 //
                 CurrentInBill = new BilInBill();
                 CurrentInBill.Id = client.InitInBillCode();
                 CurrentInBill.IBType = record.IBType;
                 CurrentInBill.IBPlanCode = record.Id;
                 CurrentInBill.SenderCode = record.SenderCode;
                 CurrentInBill.SenderName = record.SenderName;
                 CurrentInBill.RecieverCode = record.RecieverCode;
                 CurrentInBill.RecieverName = record.RecieverName;
                 CurrentInBill.WarehouseCode = record.WarehouseCode;
                 CurrentInBill.WarehouseName = record.WarehouseName;
                 CurrentInBill.BillStatus = "0";
                 CurrentInBill.IsDeactived = "0";

                 DateTime dtNow = client.GetCurrentSystemTime();
                 CurrentInBill.CreateUserName = MainForm.CurrentUser.UserName;
                 CurrentInBill.CreateUserCode = MainForm.CurrentUser.Id;
                 CurrentInBill.CreateDate = Helper.CommonHelper.GetDateString(dtNow);
                 CurrentInBill.CreateTime = Helper.CommonHelper.GetTimeString(dtNow);

                 if (!client.AddInBill(CurrentInBill))
                 {
                     MessageBox.Show("添加入库单失败！");
                     return;
                 }
             }

            ReceiveForm window = new ReceiveForm(_currentInPlanBillCode, CurrentInBill.Id);
            window.ShowDialog();
        }
        #endregion


    }
}
